package com.wyh.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wyh.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117615334";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCWYgYKnNEhdR/+pwDPI3AWND/EcnE2RoYZbwCL+UegspBqySDMlmWapITBPqYW/bRHVPNk7P/oV4J+MWIK1xDSUICHpsMR66nLghwhYcZJGOQi228Ky3QA55rVzYv0NH7oRGrDMg0i5cNPKpXMafPFIyT6OPKHfvuCM+V52cpmSqMvCYF7/mnSAcltagUthuTUjdzQbzTcU4lftwISXuiN+ZACwhNMFFHZkEo20YUc4RSuKYeQJDpJ6h75tdPpLX89rEg2Dfz/7UP31WR3ImeWv7CD5a2KC5ic5uO7sxfFgjhmO/dbiSXbitHcN51fh2Gm6/+n1ToueGlDD9RPY1z/AgMBAAECggEAQxccRuPLUTflvEBZVhAjU2SgBk5gP6ya6wHgppHh6MmBMILmhR+b2LAJIgWHJ76sDR7TL3Wkfc18xDvX16o3MP0FOn+li3st10MGtcXfJoFaVkq7cX9vE3BAXZ5EEADTlV/gmZDc13LV/Z2sEvQ58vtI5fE+EkEOh/oLKJ+HcrauXTwnDOQ9xFZ0IVqdlJo/cJLVs3lrvJyfENmCpSdjoXs9gy1sGpoO95gPNCTuYpkYSN2sEb1qHR14LhrLpsRwxwlmzhzlwrUgUuU8ZAgqD1cilBIHaVEOOLH1+dWJpbw7nzG+TRgi1eEsc5X4ouIyo4OnDZ2xN0R0SEFQrGA1EQKBgQDPPAqyZtawXGOY5RtarmAHU8thczvicTXBN2Z325vZ4g15iEPz1iry1Maf89n7lMT7+9HiCNG270vdwJnnPrEIZivqffJlQVyPhUnqMEwf7Qw/XEcmOfP51XL8jX8OTmvGhaCwZgm0OkZCe1P1G4CGdMp5MW281kdu1LU+rGd7HQKBgQC5xTLHyeol/Yo48IvlOC3sB8DMYwobXLOGcQ1aVO8TAbz9pMHUF1AzxLjLaLP0tLdK9HLQ8dTeovYvnDufUlmnyniplUM45+Jwd7/lS67O7Pys9p3rkln+IZUmxDFzJzgOESvfq0fLBptGHxPYzM0tfBRMk3d84Xvp6VucDG4hywKBgQCB4H5PurXYR76/xu5y0ZArZ2+yPlcMnqjy0/arLX4qekq5nRYBTnsSPxnQizJVQrKFvheW0rIkNSrV6mxKCfdHbUkeqBpvLRyGciGV3PkIlTa0yu9oXK+0Jvrunvs/CxTukTbY9sokItV/uu0uUzMkkTOH6tt8msqgAj0o70HXXQKBgB/pajG708c26JmjaMS1g9357vqtQL02jXhYz0xnLrJ7J/YCaXF/l71s27nFgLRWnu33VAnkoQalw9il4/AYe2JBe7zV8ThDTTi4uXQwLhVJzJYBv1sKRv9K0Y5ZQY1gfVadSIYhrDGKLv46tHQXql9X4zzMUEhZi6nA9oW19O1FAoGAEmBSqbxmgunssP3n6u7Pv1XkkYq+NIXaf6gvZRvQ+UjMRktaUannMR/yvWRCzp4767G3in9878cGfEUASobejPuV79hizdiQ7R1Udr0zCiFAaUFjvesfSY+EbVqk0uj0tRgz1wK1vR1spVx7SWtXgP9qzxwC6JCTAO9/xckgVS0=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk0mXqtySomj38Z9ZuuQIZ05rKVAr8uqsabsLW2Wq1d1Mvcviq7IHGy9q3V0WcWgMEa1COQRyOkzaTFcAw7k7MlBcJHiwGKQiJx2CtuNbApaeHabnEeEDr8hi33+TxkEH/AkqAnCdve/yR5ULfkLHbpp+o3ftD7OuTy/14wH1QO1ld9GaMXvFvWW3bWiyuAuwpxKgRjP/JohbwUkMJzXQLfHx/CeNHVt6PesvpdoSUIRkFguqTIteS5iKIcWjJHWU0jOHPPq/ypnwpV8E9mQ64w8pAKh9D1g/XSgjpfhdLfXOxb02ey5Gotoup+b2m6AXaBR9CrT3GbqPlu/KUa1icQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://0ja7vddhg5.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
